package cn.csc.module.shoppingCard.api.card;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.security.core.util.SecurityFrameworkUtils;
import cn.csc.module.shoppingCard.api.card.dto.CardMultiLockReqDTO;
import cn.csc.module.shoppingCard.api.card.dto.CardRespDTO;
import cn.csc.module.shoppingCard.convert.card.CardConvert;
import cn.csc.module.shoppingCard.dal.dataobject.card.CardDO;
import cn.csc.module.shoppingCard.service.card.CardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

import static cn.csc.module.shoppingCard.enums.ApiConstants.VERSION;

/**
 * @author liuchuang
 * @version 1.0
 * @modify
 * @since 2025/3/12 10:07
 */
@Slf4j
@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
public class CardApiImpl implements CardApi {


    @Resource
    private CardService cardService;

    @Override
    public CommonResult<List<CardRespDTO>> listOfCurrent() {
        List<CardDO> cardDOS = cardService.selectListByUserId(BigInteger.valueOf(SecurityFrameworkUtils.getLoginUserId()));
        return CommonResult.success(CardConvert.INSTANCE.convertList1(cardDOS));
    }

    @Override
    public CommonResult<Boolean> lock(BigInteger cardId, BigInteger orderId) {
        int locked = cardService.lockCard(BigInteger.valueOf(SecurityFrameworkUtils.getLoginUserId()), cardId, orderId);
        return CommonResult.success(locked > 0 ? Boolean.TRUE : Boolean.FALSE);
    }

    @Override
    public CommonResult<Boolean> multiLock(List<CardMultiLockReqDTO> list) {
        return CommonResult.success(cardService.multiLockCard(list));
    }

    @Override
    public CommonResult<Boolean> unLock(List<BigInteger> orderIds, BigInteger userId) {
        int unLocked = cardService.unLockCard(orderIds,userId);
        return CommonResult.success(unLocked > 0 ? Boolean.TRUE : Boolean.FALSE);
    }

    @Override
    public CommonResult<List<CardRespDTO>> listByCardId(List<BigInteger> cardIds) {
        log.info("[CardApiImpl][listByCardId]cardIds{}",cardIds);
        List<CardDO> list=cardService.getCardList(cardIds);
        log.info("[CardApiImpl][listByCardId]list{}",list);
        return CommonResult.success(CardConvert.INSTANCE.convertList1(list));
    }


}
